/**
 * 
 */
package com.yidu.drug.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Message;
import com.yidu.drug.model.DrugMaterialsModel;
import com.yidu.drug.model.DrugProductionModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.service.DrugMaterialsService;
import com.yidu.drug.service.DrugProductionService;
import com.yidu.drug.service.RepertoryService;

/**
 * 药品生产计划DAO
 * @author 王志伟
 * @date 2018年8月30日 上午9:11:11
 * @version 1.0
 */
@Controller
@RequestMapping("drugProduction")
public class DrugProductionAction {

	//药品生产计划的service
	@Resource
	private DrugProductionService drpService;
	
	@Resource
	private DrugMaterialsService dmService;
	
	@Resource
	private RepertoryService repService;
	//消息类
	Message m=new Message();
	
	@RequestMapping("/findByProId")
	@ResponseBody
	public Map<String, Object> findByProId(String limit, String page,String proId){
		System.out.println("进入生产计划药品查询所有的方法"+proId+"ID");
		return drpService.findByProId(limit, page, proId);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(String drpId){
		int count = drpService.deleteByPrimaryKey(drpId);
		m.setNum(count);
		return m;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Message update(String drpId,String drpNumber,String drugId){
		System.out.println("进入根据ID修改药品材料表的方法 ID：："+drpId+".."+drpNumber+".."+drugId);
		int count=1;
		DrugProductionModel drp=new DrugProductionModel();
		drp.setDrpId(drpId);
	    drp.setDrpNumber(Long.valueOf(drpNumber));
	    List<DrugMaterialsModel> dmList= dmService.findMatIdByDrugId(drugId);
	    for (DrugMaterialsModel drugMaterialsModel : dmList) {
	    	System.out.println("材料ID+"+drugMaterialsModel.getMatId()+"===========");
			RepertoryModel rep= repService.findByPwId(drugMaterialsModel.getMatId());
			System.out.println("材料数量"+rep.getRepNumber());
			Long num=(Long.valueOf(drpNumber));
			if(num*drugMaterialsModel.getDmNumber()>rep.getRepNumber()){
				count=0;
			}
		}
	    if(count>0){
	    	m.setNum(drpService.updateByPrimaryKeySelective(drp));
	 	    return m;
	    }else{
	    	m.setNum(count);
	    	return m;
	    }
	}
	
	@RequestMapping("/updateOrAdd")
	@ResponseBody
	public Message updateOrAdd(String a){
		System.out.println("进入材料配置保存");
		int count=drpService.updateOrAdd(a);
		m.setNum(count);
	    return m;
	}
	
	@RequestMapping("/findDrugIdByProId")
	@ResponseBody
	public List<DrugProductionModel> findDrugIdByProId(String proId){
		System.out.println("根据生产ID查询药品 id="+proId);
		return drpService.findDrugIdByProId(proId);
	}
	
	@RequestMapping("/addOrDelete")
	@ResponseBody
	public Message addOrDelete(String drugIds,String proId){
		System.out.println("进入生产药品计划修改 药品ID："+drugIds+" 所对应生产ID为："+proId);
		int count=drpService.addOrDelete(drugIds,proId);
		m.setNum(count);
	    return m;
	}
}
